package plantObjects;

import control.Event;
import control.Eventhandler;
import java.util.LinkedList;

/**
 * A virtual buffer and a real representative of a conveyor belt
 * @author guru
 */
public class Conveyor implements TransformingUnit{

    private int time, buffer;
    private LinkedList<Slap> bufferedSlaps = new LinkedList<Slap>();


    public Conveyor(int time, int buffer)
    {
        this.buffer = buffer;
        this.time = time;
    }

    /**
     * Put a Slap on a conveyor end get a event back
     *
     * @param Slap to put
     * @return Event when slap is finished
     */
    public void put(Slap s)
    {
        // return null & a error message to console, if the buffer is full
        if(!checkConvIsAvail()) System.err.println("Buffer of Conveyor is full! Slap can not be loaded");
        else
        {
            // add Slap to conveyor
            bufferedSlaps.add(s);
            // send event to eventhandler
            Eventhandler.add(new Event(Eventhandler.getTime() + time, this));
        }
    }
    /**
     * get the element at the end of the conveyor belt
     * @return Slap
     */
    public Slap get()
    {
        return bufferedSlaps.poll();
    }

    /**
     * Check if the conveyor has at least 1 free buffer
     * @return true if there is a free buffer / false = full
     */
    public boolean checkConvIsAvail()
    {
        if(bufferedSlaps.size() >= buffer) return false;
        return true;
    }
}
